package com.store.framework.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.store.framework.gui.model.CalcModel;
import com.store.framework.gui.view.CalcView;

public class CalcController {
	private CalcModel m_model;
	private CalcView m_view;

	public CalcController(CalcModel model, CalcView view) {
		this.m_model = model;
		this.m_view = view;
		view.addMultiplyListener(new MultiplyListener());
		view.addClearListener(new ClearListener());
	}

	public void show() {
		m_view.setVisible(true);
	}

	public void hide() {
		m_view.setVisible(false);
	}

	class MultiplyListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent action) {
			String userInput = "";
			try {
				userInput = m_view.getUserInput();
				m_model.multiply(userInput);
				m_view.setTotal(m_model.getValue());
			} catch (Exception e2) {
				m_view.showError("Bad input: '" + userInput + "'");
			}
		}
	}

	class ClearListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent action) {

			m_model.reset();
			m_view.reset();
		}

	}
}
